# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  
@software: PyCharm 
@file: calStoryOfStone.py.py 
@time: 2018/8/13 15:00
"""

'''
把红楼梦做个词云,按照贾宝玉和林黛玉形象来做
'''

import jieba
from wordcloud import WordCloud
from scipy.misc import imread


# 排除一些无关人名词语
excludes = {'什么', '一个', '我们', '那里', '你们', '如今', '说道',
            '知道', '老太太', '起来', '姑娘', '这里', '出来', '他们', '众人', '自己', '一面', '太太', '只见', '怎么', '奶奶', '两个', '没有', '不是', '不知', '这个', '听见'}
# 读取红楼梦文章按空格进行分词
with open('红楼梦.txt', 'r', encoding='gb18030') as f:
    txt = f.read()

# print(txt)
words = jieba.lcut(txt)
newtxt = ' '.join(words)

# 制作形状
mask = imread('hongloumen.png')

# 生成词云
wdcd = WordCloud(background_color='white',
                 width=800,
                 height=600,
                 font_path='msyh.ttc',
                 max_words=200,
                 max_font_size=80,
                 stopwords= excludes,
                 mask=mask,).generate(newtxt)
# 生成png图片
wdcd.to_file('红楼梦词云.png')